function two() {
  function getStyle(obj, attr) {
    if(obj.currentStyle) {
      return obj.currentStyle[attr];
    } else {
      return getComputedStyle(obj, false)[attr];
    }
  }
  function startmove(obj, json, fn) {
    clearInterval(obj.timer);
    obj.timer=setInterval(function (){
      var bStop=true;
      for(var attr in json) {
        var iCur=0;
        if(attr=='opacity') {
          iCur=parseInt(parseFloat(getStyle(obj, attr))*100);
        } else {
          iCur=parseInt(getStyle(obj, attr));
        }
        var iSpeed=(json[attr]-iCur)/2;
        iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
        if(iCur!=json[attr]) {
          bStop=false;
        }
        if(attr=='opacity') {
          obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
          obj.style.opacity=(iCur+iSpeed)/100;
        } else {
          obj.style[attr]=iCur+iSpeed+'px';
        }
      }
      if(bStop) {
        clearInterval(obj.timer);
        if(fn) {
          fn();
        }
      }
    }, 30)
  }
  function buildgame() {
    var bo1=document.getElementById("bo1");
    var bo2=document.getElementById("bo2");bo2.b=1;
    var po=document.getElementById("poi");po.a=0;
    var a=0,b=12;
    for(var i=0;i<16;i++){
      var bl1=document.createElement("div");
      var bl2=document.createElement("div");
      if(i>3&&i<8){
        a=4;b=124;
      }else if(i>7&&i<12){
        a=8;b=236;
      }else if(i>11){
        a=12;b=348;
      }
      bl1.style.left=112*(i-a)+12+"px";bl1.style.top=b+"px";
      bl2.po=0;bl2.p2=0;bl2.on=1;
      bl2.le=112*(i-a)+12;bl2.to=b;
      bl2.style.left=bl2.le+"px";bl2.style.top=b+"px";
      bo1.appendChild(bl1);bo2.appendChild(bl2);
    }
    document.onkeydown=function (e) {
      var ev=e.keyCode||event.keyCode;
      if(bo2.b==1){
        bo2.b=0;
        if(ev==37){
          takepoints(1);
        }else if(ev==38){
          takepoints(2);
        }else if(ev==39){
          takepoints(3);
        }else if(ev==40){
          takepoints(4);
        }

      }

    };
    document.getElementById("le").onclick=function(){
      if(bo2.b==1){
        bo2.b=0;
        takepoints(1);
      }
    };
    document.getElementById("to").onclick=function(){
      if(bo2.b==1){
        bo2.b=0;
        takepoints(2);
      }
    };
    document.getElementById("ri").onclick=function(){
      if(bo2.b==1){
        bo2.b=0;
        takepoints(3);
      }
    };
    document.getElementById("bo").onclick=function(){
      if(bo2.b==1){
        bo2.b=0;
        takepoints(4);
      }
    };
    document.getElementById("newa").onclick=newgame;

    newdiv(2);
  }
  function addpoi() {
    var po=document.getElementById("poi");
    po.a+=1;
    po.innerHTML="你的分数："+po.a;
  }
  function newgame(){
    var bo2=document.getElementById("bo2");
    var bl2=bo2.getElementsByTagName("div");
    var ov=document.getElementById("over");
    startmove(ov,{opacity:0});
    for(var i=0;i<bl2.length;i++){
      bl2[i].po=bl2[i].p2=0;
      bl2[i].innerHTML="";bl2[i].style.backgroundColor="rgb(204, 192, 179)";
    }
    var po=document.getElementById("poi");
    po.a=0;
    po.innerHTML="你的分数："+po.a;
    newdiv(2);
  }
  function newdiv(n){
    var bo2=document.getElementById("bo2");
    var bl2=bo2.getElementsByTagName("div");
    var ys={2: "rgb(238, 228, 218)",4:"rgb(237, 224, 200)"};
    for(var m=0;m<n;m++){
      var ano=[];
      for(var i=0;i<bl2.length;i++){
        if(bl2[i].po==0){ano.push(bl2[i]);}
      }
      var r1=Math.floor(Math.random()*ano.length);
      ano[r1].innerHTML=ano[r1].po=(Math.random()>0.5?2:4);
      ano[r1].style.backgroundColor=ys[ano[r1].po];
    }
  }
  function takepoints(k) {
    var bo2=document.getElementById("bo2");
    var bl2=bo2.getElementsByTagName("div");bo2.on2=0;bo2.c=0;
    var a=0,i=0,s1=0,s2=0,s3=0,s4=1,s5=0,t1=0,t2=0,t3=0,t4=0;
    if(k==1){//左
      s1=4;s2=4;t2=4;t3=1;
    }else if(k==2){//上
      s1=1;t2=16;t3=4;
    }else if(k==3){//右
      s4=0;s1=-4;t1=15;s5=-4;t4=11;s3=1;t3=-1;
    }else if(k==4){//下
      s4=0;s1=-1;t1=15;t4=-1;s3=1;t3=-4;
    }
    var arr=[[],[],[],[]];
    for(i;i<4;i++){
      for(a=i*s1+t1;a*s4+i*s5+t4<i*s2+t2+a*s3;a+=t3){
        if(arguments.length==1){
          if(bl2[a].po>0){
            bl2[a].p2=bl2[a].po;
            arr[i].push(bl2[a]);
          }
        }else{
          if(bl2[a].on==1){
            bo2.on2+=1;
          }
        }
      }
      if(arguments.length==1){
        if(arr[i].length==1){
          if(arr[i][0]!=bl2[i*s1+t1]){
            movediv(arr,i,i*s1+t1,0,2);bo2.c=1;
          }
        }else if(arr[i].length==2){
          if(arr[i][0].p2==arr[i][1].p2){
            if(arr[i][0]!=bl2[i*s1+t1]){
              movediv(arr,i,i*s1+t1,0);bo2.c=1;
            }
            movediv(arr,i,i*s1+t1,1,1);bo2.c=1;
          }else{
            if(arr[i][0]!=bl2[i*s1+t1]){
              movediv(arr,i,i*s1+t1,0,2);bo2.c=1;
            }
            if(arr[i][1]!=bl2[i*s1+t1+t3]){
              movediv(arr,i,i*s1+t1+t3,1,2);bo2.c=1;
            }
          }
        }else if(arr[i].length==3){
          if(arr[i][0].p2==arr[i][1].p2){
            if(arr[i][0]!=bl2[i*s1+t1]){
              movediv(arr,i,i*s1+t1,0);bo2.c=1;
            }
            movediv(arr,i,i*s1+t1,1,1);bo2.c=1;
            movediv(arr,i,i*s1+t1+t3,2,2);bo2.c=1;
          }else{
            if(arr[i][0]!=bl2[i*s1+t1]){
              movediv(arr,i,i*s1+t1,0,2);bo2.c=1;
            }
            if(arr[i][2].p2==arr[i][1].p2){
              if(arr[i][1]!=bl2[i*s1+t1+t3]){
                movediv(arr,i,i*s1+t1+t3,1);bo2.c=1;
              }
              movediv(arr,i,i*s1+t1+t3,2,1);bo2.c=1;
            }else{
              if(arr[i][1]!=bl2[i*s1+t1+t3]){
                movediv(arr,i,i*s1+t1+t3,1,2);bo2.c=1;
              }
              if(arr[i][2]!=bl2[i*s1+t1+t3*2]){
                movediv(arr,i,i*s1+t1+t3*2,2,2);bo2.c=1;
              }
            }
          }
        }else if(arr[i].length==4){
          if(arr[i][0].p2==arr[i][1].p2){
            movediv(arr,i,i*s1+t1,1,1);bo2.c=1;
            if(arr[i][2].p2==arr[i][3].p2){
              movediv(arr,i,i*s1+t1+t3,2);bo2.c=1;
              movediv(arr,i,i*s1+t1+t3,3,1);bo2.c=1;
            }else{
              movediv(arr,i,i*s1+t1+t3,2,2);bo2.c=1;
              movediv(arr,i,i*s1+t1+(t3*2),3,2);bo2.c=1;
            }
          }else if(arr[i][2].p2==arr[i][1].p2){
            movediv(arr,i,i*s1+t1+t3,2,1);bo2.c=1;
            movediv(arr,i,i*s1+t1+(t3*2),3,2);bo2.c=1;
          }else if(arr[i][2].p2==arr[i][3].p2){
            movediv(arr,i,i*s1+t1+(t3*2),3,1);bo2.c=1;
          }
        }
      }
    }
    if(arguments.length!=1){
      if(bo2.on2==16){
        newdiv(1);bo2.b=1;
      }
    }else if(arguments.length==1&&bo2.c==0){
      bo2.b=1;
      var al=0;
      for(let i=0;i<4;i++){
        if(arr[i].length>3){
          al+=1;
        }
      }
      if(al==4){
        gamenow(arr);
      }
    }
  }
  function gamenow(arr) {
    var a=0;var b=0;
    for(var i=0;i<4;i++){
      if(arr[i][0].po!=arr[i][1].po&&arr[i][2].po!=arr[i][1].po&&arr[i][2].po!=arr[i][3].po){
        a+=1;
      }
      if(arr[0][i].po!=arr[1][i].po&&arr[1][i].po!=arr[2][i].po&&arr[2][i].po!=arr[3][i].po){
        b+=1;
      }
    }
    if(b==4&&a==4){
      var ov=document.getElementById("over");
      var po=document.getElementById("poi");
      ov.innerHTML="Game Over!<br/>"+"You got "+po.a+" points.";
      startmove(ov,{opacity:50})
    }
  }
  function movediv(arr,i,a,b,c) {
    var bo2=document.getElementById("bo2");
    var bl2=bo2.getElementsByTagName("div");
    var ys={0:"rgb(204, 192, 179)",2: "rgb(238, 228, 218)",4:"rgb(237, 224, 200)"
      ,8:"rgb(242, 177, 121)",16:"rgb(245, 149, 99)",32:"rgb(246, 126, 95)"
      ,64:"rgb(246, 94, 59)",128:"rgb(237, 207, 114)",256:"#edcc61"
      ,512:"rgb(198,88,249)",1024:"rgb(11,248,152)",2048:"rgb(245,2,174)"
      ,4096:"rgb(47,15,180)",8192:"rgb(214,17,33)"};
    var l=arr[i][b];var p=bl2[a];
    l.style.zIndex="1000";p.on=0;
    startmove(l,{left:p.le,top:p.to},function () {
      l.innerHTML="";l.style.left=l.le+"px";l.style.top=l.to+"px";l.po=0;
      l.style.zIndex="100";l.style.backgroundColor=ys[0];
      if(c==1){
        p.po=(l.p2)*2; addpoi();p.innerHTML=p.po;
      }else if(c==2){
        p.po=l.p2;p.innerHTML=p.po;
      }
      if(p.po>-1){
        p.style.backgroundColor=ys[p.po];
      }
      p.on=1;
      takepoints(1,1);
    });
  }
  buildgame();
}
export default two
